const path = require('path')
const webpack = require('webpack')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
  entry: './src/main.js',
  output: {
    filename: 'js/[name].[contenthash:6].js',
    path: path.join(__dirname, 'dist')
  },
  module: {
    rules: [
      {
        test: /.vue$/,
        use: ['vue-loader']
      },
      {
        test: /.css$/,
        use: ['style-loader', 'css-loader']
      },
      {
        test: /.less$/,
        use: ['style-loader', 'css-loader', 'less-loader']
      },
      {
        test: /.png$/,
        use: {
          loader: 'url-loader',
          options: {
            limit: 10 * 1024, // 10kb
            esModule: false,
            name: 'images/[name].[contenthash:6].[ext]'
          }
        }
      },
      {
        test: /.js$/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env']
          }
        },
        exclude: /node_modules/
      },
      {
        test: /.js$/,
        use: {
          loader: 'eslint-loader'
        },
        exclude: /node_modules/,
        enforce: 'pre'
      }
    ]
  },
  plugins: [
    // 全局成员注入
    new webpack.DefinePlugin({
      BASE_URL: '"./"'
    }),
    // vue loader 插件，必须
    new VueLoaderPlugin(),
    // 生成 html
    new HtmlWebpackPlugin({
      title: 'Vue Webpack Sample',
      template: './public/index.html'
    })
  ]
}
